<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>15.矩阵画点</title>
	<style>
		canvas {
            background-color: rebeccapurple;
		}
	</style>
	<script src="../utils/index.js"></script>
	<link rel="stylesheet" href="../utils/ui.css">
</head>
<body>
	<div id="ui">
		<div class="x"></div>
		<div class="y"></div>
	</div>
	<canvas id="gl" style="background-color: rebeccapurple;"></canvas>
	<script type="x-shader/x-vertex" id="vertex">
		uniform mat3 u_matrix;
		void main(){
			vec3 a_position=vec3(0.,0.,1.);
            gl_PointSize = 4.0; 
			vec3 position = (u_matrix*a_position );
			gl_Position = vec4(position.xy*vec2(1,-1),position.z,  1);
		}
	</script>
	
	<script type="x-shader/x-fragment" id="fragment">
		void main(){
			gl_FragColor =vec4(0.0,1.0,1.0,1.0);
		}
	</script>
	<script>
		 /** @type {HTMLCanvasElement} */   
		const canvas=document.getElementById("gl")
		const gl=canvas.getContext("webgl")
		canvas.width=1000
		canvas.height=1000
		//流程 1.创建着色器 编译 链接
		const vertex=document.getElementById("vertex").textContent
		const fragment=document.getElementById("fragment").textContent
		
		const vertexShader=gl.createShader(gl.VERTEX_SHADER)
		const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)
		gl.shaderSource(vertexShader,vertex)
		gl.shaderSource(fragmentShader,fragment)
		gl.compileShader(vertexShader)
		gl.compileShader(fragmentShader)
		const program=gl.createProgram()
		gl.attachShader(program,vertexShader)
		gl.attachShader(program,fragmentShader)
		gl.linkProgram(program)
		gl.useProgram(program)
		gl.viewport(0,0,1000,1000)


		const u_matrix=gl.getUniformLocation(program,"u_matrix")
		const translation=[0,0]
		console.log("u_matrix:",u_matrix);
		let m3=webglUtils.Mat3
		let matrix=m3.translation(...translation)
		gl.uniformMatrix3fv(u_matrix,false,matrix)
		webglUtils.setupSlider(".x",{
			min:-1,
			max:1,
			value:0,
			step:0.01,
			handle:(e,v)=>{
				translation[0]=v.value
				matrix=m3.translation(...translation)
				draw()
			}
		})
		webglUtils.setupSlider(".y",{
			min:-1,
			max:1,
			value:0,
			step:0.01,
			handle:(e,v)=>{
				translation[1]=v.value
				matrix=m3.translation(...translation)
				draw()
			}
		})
		function draw(){
			gl.uniformMatrix3fv(u_matrix,false,matrix)
			gl.drawArrays(gl.POINTS, 0, 1);
		}
		draw()

	</script>
</body>
</html>
